package com.dz.lm.common.util;

import com.alibaba.fastjson.JSON;
import com.dz.lm.common.utils.APIV3SignDemo;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.XmlResolve;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/21 11:17;
 */
public class TecentVideo {
    private static int socketTimeout = 10000;// 连接超时时间，默认10秒
    private static int connectTimeout = 30000;// 传输超时时间，默认30秒
    private static final Logger logger = LoggerFactory.getLogger(TecentVideo.class);
    private static InputStream in;
    private static Properties p;
    static {
        try {
            in = TecentVideo.class.getClassLoader().getResourceAsStream("tecent_video.properties");
            p = new Properties();
            p.load(in);
        } catch (FileNotFoundException e) {
            logger.error("WxPay Not Found File :tecent_video.properties");
            e.printStackTrace();
        } catch (IOException e) {
            logger.error("WxPay IOException");
            e.printStackTrace();
        }
    }
    public static int delete(String fileId) {
        int ret =0;
        try {
            Credential cred = new Credential("AKIDB9sNdeI0LrAE3ePC8rXzDBvQghUEn14s", "kGWYVEhutd9PSRDl4A0cy6B1bw2tVryv");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("vod.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            VodClient client = new VodClient(cred, "", clientProfile);
            Map param= new HashMap();
            param.put("FileId",fileId);
            //String params = "{\"FileId\":\"5285890800013548426\"}";
           String params =JSON.toJSONString(param);
            DeleteMediaRequest req = DeleteMediaRequest.fromJsonString(params, DeleteMediaRequest.class);
            DeleteMediaResponse resp = client.DeleteMedia(req);
            System.out.println(DeleteMediaRequest.toJsonString(resp));
            ret =1;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return  ret;
    }
    /**
     * 删除视频
     * @param fileId 视频id
     * @return
     * @throws Exception
     */
    public static int DeleteVideo(String fileId) throws Exception {
        Map inParam = APIV3SignDemo.getSinature(fileId);
        int ret =0;
        // SecretId
        String SecretId = p.getProperty("SecretId");
        String Action = p.getProperty("Action");
        String Version = p.getProperty("Version");
        String Language = p.getProperty("Language");

        //回调URL
        //删除视频url
        String unifiedorder_url = p.getProperty("url");
        //参数组合
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("SecretId", SecretId);
        param.put("Action", Action);
        param.put("Version", Version);
        param.put("Language", Language);
        param.put("FileId", fileId);
        param.put("Language", Language);
        param.put("Token", "");
        param.put("Region", "");
        param.put("Timestamp", inParam.get("Timestamp"));
        param.put("Signature", inParam.get("signature"));
        param.put("Nonce", inParam.get("Nonce"));
        String xmlParam = XmlResolve.callMapToXML(param);
        PostMethod postMethod = new PostMethod(unifiedorder_url);

        try {
            postMethod.getParams().setContentCharset("UTF-8");
            postMethod.addRequestHeader("Connection", "close");
            postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
            postMethod.setRequestBody(xmlParam);
            postMethod.setParameter("SecretId", SecretId);
            postMethod.setParameter("Action", Action);
            postMethod.setParameter("Version", Version);
            postMethod.setParameter("Language", Language);
            postMethod.setParameter("FileId", fileId);
            postMethod.setParameter("Language", Language);
            postMethod.setParameter("Token", "");
            postMethod.setParameter("Region", "");
            postMethod.setParameter("Timestamp", inParam.get("Timestamp").toString());
            postMethod.setParameter("Signature", inParam.get("signature").toString());
            postMethod.setParameter("Nonce", inParam.get("Nonce").toString());
            HttpClient httpClient = new HttpClient();
            int statusCode = httpClient.executeMethod(postMethod);
            String response = postMethod.getResponseBodyAsString();
            logger.info("getToken statusCode: " + statusCode + ", body: "+ response);

            if(200 == statusCode && !StringUtils.isEmpty(response)){

            }else{
                throw new Exception("微信支付返回结果解析错误");
            }
        } catch (Exception e) {
            logger.info("微信支付返回错误" + e.getStackTrace());
            throw new Exception("微信支付返回错误", e);
        } finally {
            // 释放连接
            postMethod.releaseConnection();
        }
        return ret;
    }
}
